/* eslint-disable */

import { create } from 'dva-core';
import createLoading from 'dva-loading';
import { Provider, connect } from '@tarojs/redux';
import { storage } from '@doctorwork/saga';
import { createLogger } from 'redux-logger';
import models, { merge, except } from './model';
import { isHybrid } from './model/api';
import { setupTrace } from './model/business/request';

let app;
let store;
let dispatch;

function createApp(opt) {
    opt.models = models;
    if (process.env.DEBUG) {
        opt.onAction = [createLogger({
            predicate: (state, action) => {
                // 不输出包含 @@ 的 系统 action
                return action.type.indexOf("@@") == -1;
            }
        })];
    }
    app = create(merge(opt));
    app.use(
        createLoading({
            except
        })
    );

    if (!global.registered) opt.models.forEach(model => app.model(model));
    global.registered = true;
    app.start();

    store = app._store;
    app.getStore = () => store;

    app.dispatch = dispatch = store.dispatch;
    global.dispatch = app.dispatch;

    return app;
}

export default {
    createApp,
    getDispatch() {
        return app.dispatch;
    }
};
// 设置场景值
export const setScene = function(params) {
    if (!isHybrid) {
        store.dispatch(storage.set('scene', params.scene));
    }

    setupTrace(params, store.dispatch);
};
